<?php
require "../common/user_online.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<title>EbookAZ</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php require "../components/head.php" ?>
<style type="text/css">
	.over
	{
		cursor:pointer;
		padding-left:15px;
		color: #0000FF;
	}
	
	.overactive
	{
		color:#FF0000;
		padding-left:15px;
		cursor:pointer;
	}
</style>
<script type="text/javascript" src="../scripts/search-script.js"></script>
<script type="text/javascript">
	$("document").ready(function (){
		$("#show").bind("click",function () {
			
			$("#continue_search").html("");
            $("#search_form").slideDown(2000);
            });
		
		$("b").click(function(){
			$("b[class=overactive]").attr("class","over");
			$(this).attr("class","overactive");
		});
	});
	

	
</script>
</head>
<body onload="MakeCurrentPage(2)">
<div id="container">
	<?php require "../components/user_menu.php" ?>
	<?php require "../components/user_header.php" ?>
	<div id="content">
		<?php require "../components/content_left.php"?>
		<div id="content_right" align="center">
		<h1 align="center">TRA CỨU SÁCH</h1>
        
			<script type="text/javascript">
				for(var i = 65;i <= 90;i++)
				{
					document.write("<b class='over' onclick='" + 'GetBookByKey("' + String.fromCharCode(i) + '",1)' + "'>");
					document.write(String.fromCharCode(i));
					document.write("</b>");
				}
			</script>
			<br /><div id="divWait"></div>
			<input type='hidden' id='hiddenAlphaBeta'/>
		
        
        <br /><div id="continue_search"></div>
        <br />
        <div id="search_form" align="center">
        <form action="" method="get">
        <table border="0" class="normal">
        <tr>
            <td>Tên sách</td>
            <td><input type="text" name="txtBookName" maxlength="200" size="50" tabindex="1"/>
            
			Thể loại
            <select name="BookTypeID" tabindex="2">
            <option value="0">Chọn</option>
            <?php
                $mysqli = new mysqli($host,$username,$password,$dbname);
                if(mysqli_connect_errno())
                {
                    printf("Connect failed: %s\n",mysqli_connect_error());
                    exit();
                }
                @$mysqli->query("set names 'utf8'");
                $sql_GetAllEBookType = "CALL sp_GetAllEBookType()";
                $result_EBookType = @$mysqli->query($sql_GetAllEBookType);
                if($mysqli->errno)
                {
                    die("Execute failed: ".$mysqli->errno.": ".$mysqli->error);
                }
                while($row = $result_EBookType->fetch_object())
                {
                    printf("<option value='%d'>%s</option>",$row->ID,$row->Name);
    
                }
                $mysqli->close();
            ?>
            </select>
            </td>
        </tr>
        <tr>
            <td>Tác giả</td>
            <td><input type="text" name="txtAuthor" maxlength="100" size="50" tabindex="3"/></td>     
        </tr>
		<tr>
			<td>NXB</td>
			<td><input type="text" name="txtPublisher" size="50"  tabindex="4"/></td>
		</tr>
        <tr>
            <td>Lĩnh vực</td>
			<td>
            <select name="FieldID" onchange="GetCategoryByField(this.value,0)">
            <?php
                $mysqli = new mysqli($host,$username,$password,$dbname);
                if(mysqli_connect_errno())
                {
                    printf("Connect failed: %s\n",mysqli_connect_error());
                    exit;
                }
                @$mysqli->query("set names 'utf8'");
                $sql_GetAllField = "CALL sp_GetAllField()";
                $result_Field = @$mysqli->query($sql_GetAllField);
                if($mysqli->errno)
                {
                    die("Execute failed: ".$mysqli->errno.": ".$mysql->error);
                }
				printf("<option value='0' name='fieldoption'>Chọn lĩnh vực</option>");
                while($row = $result_Field->fetch_object())
                {
                    printf("<option value='%d' name='fieldoption'>%s</option>",$row->ID,$row->Name);
                }
                $result_Field->data_seek(0);
                while($first_row = $result_Field->fetch_object())
                {
                    $first_row_ID = $first_row->ID;
                    break;
                }
                $mysqli->close();
            ?>
            </select>
            </td>
			</tr>
			<tr>
            <td>Danh mục</td>
			<td id="divCategoryID">
				<?php
				require "ResponseFromServer.php";
				GetCategoryByField($first_row_ID);
				?>
           
            </td>
        </tr>
        <tr>
            <td>Mana</td>
            <td>
            <select name="isGreaterthan">
            	<option value="1">Lớn hơn</option>
                <option value="0">Nhỏ hơn</option>
            </select>
            <input type="text" name="txtMana" size="10" value="0"/>
            </td>
        </tr>
        <tr>
            <td>Đánh giá</td>
            <td>
            <input type="text" name="txtMinRatingPoint" size="2" maxlength="2" value="0"/>
            &nbsp;đến&nbsp;
            <input type="text" name="txtMaxRatingPoint" size="2" maxlength="2" value="5"/>
            &nbsp;điểm
            </td>
        </tr>
        <tr>
        	<td>Mô tả</td>
            <td>
	            <textarea name="txtDescription" style="width:450px;height:100px" ></textarea>
            </td>
        </tr>
        <tr height="40" valign="bottom">
            <td style="text-align:center;" colspan="2">
            <input type="submit" value="Tìm kiếm" name="btTimKiem" id="btTimKiem"/>
            <input type="reset" value="Hủy bỏ" onclick="resetform()"/>
            </td>
        </tr>
        </table>
        </form>
        </div>		
		
        <div id="search_result">
        <!-- Xu ly tra cuu -->
        <!-- Tra tu khoa ra giao dien nguoi dung -->
		<?php
			if(isset($_GET['btTimKiem']))
			{
				printf("<script type='text/javascript'>");
				$bookname = $_GET['txtBookName'];
				printf("ShowBookName('%s');",$bookname);
				
				
				$booktypeid = $_GET['BookTypeID'];
				printf("ShowBookType(%d);",$booktypeid);
				
				$author = $_GET['txtAuthor'];
				printf("ShowAuthor('%s');",$author);
				
				$publisher = $_GET['txtPublisher'];
				printf("ShowPublisher('%s');",$publisher);
				
				$fieldid = 0;
				if(isset($_GET['FieldID']))
				{
					$fieldid = $_GET['FieldID'];
					printf("ShowFieldID(%d);",$fieldid);
					
				}
				$catid = 0;
				if(isset($_GET['CategoryID']))
				{
					$catid = $_GET['CategoryID'];
					printf("ShowCategoryID(%d,%d);",$fieldid,$catid);
				}
				$mana = 0;
				if($_GET['txtMana'] > $mana)
					$mana = $_GET['txtMana'];
				
				$isgreaterthan = $_GET['isGreaterthan'];
				printf("ShowMana(%d,%d);",$isgreaterthan,$mana);
				
				$minRatingPoint = 0;
				if($_GET['txtMinRatingPoint'] > $minRatingPoint)
					$minRatingPoint = $_GET['txtMinRatingPoint'];
				$maxRatingPoint = 10;
				if($_GET['txtMaxRatingPoint'] > $minRatingPoint)
					$maxRatingPoint	 = $_GET['txtMaxRatingPoint'];
				
				printf("ShowMinRatingPoint(%d);",$minRatingPoint);
				printf("ShowMaxRatingPoint(%d);",$maxRatingPoint);
				
				$description = $_GET['txtDescription'];
				printf("ShowDescription('%s');",$description);
				
				print("hideform();");
				
				printf("</script>");
				
				/***** Xu ly su kien tim kiem *****/
				 
				// Ket noi CSDL
				
				require "../common/db.inc";
				$mysqli = new mysqli($host,$username,$password,$dbname);
				$mysqli_count = new mysqli($host,$username,$password,$dbname);
				if(mysqli_connect_errno())
				{
					printf("Connect failed: %s\n",mysqli_connect_error());
					exit();
				}
				$mysqli->query("set names 'utf8'");
				
				$bookname = $mysqli->real_escape_string($bookname);
				$booktypeid = $mysqli->real_escape_string($booktypeid);
				$description = $mysqli->real_escape_string($description);
				$author = $mysqli->real_escape_string($author);
				$publisher = $mysqli->real_escape_string($publisher);
				$catid = $mysqli->real_escape_string($catid);
				$fieldid = $mysqli->real_escape_string($fieldid);
				
				$books_per_page = 12;
				$numdisplay = 5;
				$current_page = 1;
				if(isset($_GET['page']))
					$current_page = $mysqli->real_escape_string($_GET['page']);
		
				$start = $books_per_page * ($current_page - 1);
				$limit = $books_per_page;
				$sql_GetEBookBySearch = "CALL sp_GetEbookBySearch('$bookname',$booktypeid,'$description','$author','$publisher',$catid,$fieldid,$isgreaterthan,$mana,$minRatingPoint,$maxRatingPoint,$start,$limit)";
				
				$sql_CountGetEBookBySearch = "CALL sp_CountGetEBookBySearch('$bookname',$booktypeid,'$description','$author','$publisher',$catid,$fieldid,$isgreaterthan,$mana,$minRatingPoint,$maxRatingPoint)";
				
				$result = @$mysqli->query($sql_GetEBookBySearch);
				$count_result = @$mysqli_count->query($sql_CountGetEBookBySearch);
				if($mysqli->errno)
				{
					die($mysqli->errno.": ".$mysqli->error);
				}
				$user = $_SESSION['user_id'];
				print("<table width='100%' cellspacing='6'>");
				while($row = $result->fetch_object())
				{
					print("<tr>");
					printf("<td valign='top' align='center' width='%d'><a href='%s'><img src='%s' height='150' width='100' /></a><br/><a href='%s'>%s</a><br/>",25, 'XemChiTietSach.php?id='.$row->ID,$image.$row->CoverImage,'XemChiTietSach.php?id='.$row->ID,$row->Title);
					if($_SESSION['user_mana'] >= $row->Mana)
						echo "<input type='button' value='Tải về' onClick='download(\"$user\", \"$row->ID\", \"$row->FileUpload\")'></input>";
					else
						echo "<input type='button' disabled='disabled' value='Tải về'></input>";
					echo "</td>";
					$i = 0;
					while($i < 3 && $row2 = $result->fetch_object())
					{
						printf("<td valign='top' align='center' width='%d'><a href='%s'><img src='%s' height='150' width='100' /></a><br/><a href='%s'>%s</a><br/>",25, 'XemChiTietSach.php?id='.$row2->ID,$image.$row2->CoverImage,'XemChiTietSach.php?id='.$row2->ID,$row2->Title);
						if($_SESSION['user_mana'] >= $row2->Mana)
							echo "<input type='button' value='Tải về' onClick='download(\"$user\", \"$row2->ID\", \"$row2->FileUpload\")'></input>";
						else
							echo "<input type='button' disabled='disabled' value='Tải về'></input>";
						echo "</td>";
						$i++;
					}
					print("</tr>");
				}
				print("</table>");
				
				$num_rows = 0;
				while($row = $count_result->fetch_object())
				{
					$num_rows = $row->count_result;
				}
				if($num_rows == 0)
				{
					print("<center><font color='red' size='3'>Không có sách thỏa từ khóa bạn đang tìm</font></center>");
				}
				else
				{
					include "../common/paging.php";
					echo paging("tracuu.php",$books_per_page, $numdisplay, $num_rows, $current_page,'&txtBookName='.$bookname.'&BookTypeID='.$booktypeid.'&txtAuthor='.$author.'&txtPublisher='.$publisher.'&FieldID='.$fieldid.'&CategoryID='.$catid.'&isGreaterthan='.$isgreaterthan.'&txtMana='.$mana.'&txtMinRatingPoint='.$minRatingPoint.'&txtMaxRatingPoint='.$maxRatingPoint.'&txtDescription='.$description.'&btTimKiem=1');
				}
				$mysqli->close();
				$mysqli_count->close();
			}
		?>
        </div>
	  </div>
	</div>
	<?php require "../components/footer.php" ?>
</div>
</body>
</html>


